package com.wnxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnxy.bean.Color;
import com.wnxy.bean.Goods;
import com.wnxy.bean.Version;
import com.wnxy.mapper.GoodsMapper;
import com.wnxy.service.GoodsService;
import com.wnxy.service.vo.GoodsDetail;
import com.wnxy.util.DBUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public  class GoodsServiceImpl implements GoodsService {
    @Override
    public GoodsDetail findGoodsById(int id) {
        SqlSession sqlSession=DBUtil.openSession();

        GoodsMapper mapper=sqlSession.getMapper(GoodsMapper.class);

        Goods goods=mapper.selectByPrimaryKey(id);

        List<Color>   colorList=mapper.selectGoodsColor(id);

        List<Version> versionList=mapper.selectGoodsVersion(id);



        for(Version version :versionList){
            System.out.println(version);
        }

        GoodsDetail  gd=new GoodsDetail();

        gd.setGoods(goods);

        gd.setColorList(colorList);

        gd.setVersionList(versionList);

        return  gd;

    }

    @Override
    public PageInfo<Goods> listGoodsPage(Integer currentPage, Integer pageSize) {

        SqlSession sqlSession=DBUtil.openSession(true);

        GoodsMapper mapper=sqlSession.getMapper(GoodsMapper.class);

        PageHelper.startPage(currentPage,pageSize);

        List<Goods> list=mapper.selectAll();

        PageInfo<Goods>  info=new PageInfo<>(list);


        sqlSession.close();


        return info;


    }
}
